import commands
import codecs
import time
import sys
import re
import rmcomment

def autoDeob():
	# chrome="/home/ruian/Documents/Webcrawler/chromium/home/src_tarball/tarball/chromium/src/out/Release/chrome "
	chrome = "/home/ruian/chromium/src/out/Release/chrome "
	# option=" --no-sandbox -incognito "
	option = " --allow-outdated-plugins"


	ustopfile = '../../data/TempData/URLs/alexaustop'
	outdir = '../../data/TempData/JSFileWComments/'
	outpart = ' > ' + outdir
	back = ' &'

	if len(sys.argv) == 2:
		dir=sys.argv[1]
		ustopfile = dir + '/data/TempData/URLs/alexaustop'
		outdir = dir + '/data/TempData/JSFileWComments/'

	ustop = codecs.open(ustopfile, encoding = 'utf-8', mode = 'r')

	# set standard buffer to 0
	cmdstdbuf = 'stdbuf -o0 '
	# sleep for some time
	cmdsleep = 'sleep 10'
	# kill chrome and restart
	cmdkillchrome = 'killall chrome'

	for line in ustop:
		website = line.split(',')[1][1:-1]
		print website
		outfilename = website.split('.')[0] + '.js'
		cmddeob = chrome + option + website + outpart + outfilename + back
		print cmddeob
		commands.getoutput(cmdstdbuf + cmddeob + '\n' + cmdsleep + '\n' + cmdkillchrome)

	ustop.close()


if __name__=="__main__":
	autoDeob()
	rmcomment.main()
	
